package com.hspedu.socket.MyTCPFileCopy;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author DL5O
 * @version 1.0
 */
public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8888);
//        String saveFilePath = "src\\bg.png";
        String saveFilePath = "src\\软件.zip";
        System.out.println("等待客户端连接...");
        //等待客户端连接
        Socket socket = serverSocket.accept();
        if(socket != null){
            System.out.println("客户端连接成功\n客服端ip地址:" + socket.getInetAddress());
        }

        //从管道中读取
        InputStream inputStream = socket.getInputStream();
        BufferedOutputStream bi = new BufferedOutputStream(new FileOutputStream(saveFilePath));
        StreamUtils.outputFile(inputStream,bi);

        //读取成功，发送消息给服务端
        String info = "服务端:读取文件成功";
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write(info.getBytes());
        outputStream.flush();
        socket.shutdownOutput();

        bi.close();
        inputStream.close();
        socket.close();
        System.out.println("服务端退出");
    }
}
